/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bice.backbean;

import com.bice.entity.Post;
import com.bice.entity.User;
import com.bice.service.PostService;
import com.bice.service.UserService;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

    
/**
 *
 * @author sirius
 */
@ManagedBean
@ViewScoped
public class CanalBean extends BackBean {
 
    
    private UserService service;
    private PostService postService;
    private User userTarget;
    private Post post;
    
    public CanalBean() {
        post = new Post();
    }

    public User getUserTarget() {
        return userTarget;
    }
    
    public void setUserTarget(User user) {
        this.userTarget = user;
    }
    
    public boolean isLoggedIn() {
        return getSessionBean().isLogged();
    }

    public void send() {
        post.setCreated(new Date());
        post.setUser(userTarget);
        post.setFromUser(getSessionBean().getUser());
        post.setCode(post.getCreated().toString());
        getPostService().save(post);
        post = new Post();
    }

    public boolean isSelectedTargetSelected() {
        return userTarget != null;
    }
    
    public List<User> getContacts() {
        return getUserService().getContacts(getSessionBean().getUser().getNickname());
    }
    
    public Post getPost() {
        return post;
    }

    public void setPost(Post post) {
        this.post = post;
    }
    
    public List<Post> getMainPosts() {
        return getPostService().getConversationDate(userTarget, getSessionBean().getUser());
    }
    
    public void clearTarget() {
        userTarget = null;
    }
    
    public boolean isTargetSelected() {
        return userTarget != null;
    }
    
    public UserService getUserService() {
        if(service == null) {
            service = (UserService) getSpringBean(UserService.class);
        }
        return  service;
    }
    
    public PostService getPostService() {
        if(postService == null) {
            postService = (PostService) getSpringBean(PostService.class);
        }
        return  postService;
    }
    
}